package com.farmer.project.controller;

import com.farmer.common.entity.Project;
import com.farmer.common.result.Result;
import com.farmer.project.dto.DonationDTO;
import com.farmer.project.service.ProjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 扶贫项目控制器
 */
@Slf4j
@RestController
@RequestMapping("/project")
@Tag(name = "扶贫项目管理", description = "扶贫项目和捐赠相关接口")
public class ProjectController {

    @Autowired
    private ProjectService projectService;

    /**
     * 获取扶贫项目列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取扶贫项目列表", description = "分页获取扶贫项目列表")
    public Result<?> getProjectList(@RequestParam(defaultValue = "1") Integer pageNum,
                                    @RequestParam(defaultValue = "10") Integer pageSize,
                                    @RequestParam(required = false) String status,
                                    @RequestParam(required = false) String keyword) {
        return projectService.getProjectList(pageNum, pageSize, status, keyword);
    }

    /**
     * 获取项目详情
     */
    @GetMapping("/{projectId}")
    @Operation(summary = "获取项目详情", description = "根据项目ID获取项目详细信息")
    public Result<?> getProjectById(@PathVariable Long projectId) {
        return projectService.getProjectById(projectId);
    }

    /**
     * 添加扶贫项目
     */
    @PostMapping("/add")
    @Operation(summary = "添加扶贫项目", description = "创建新的扶贫项目")
    public Result<?> addProject(@Valid @RequestBody Project project) {
        log.info("添加扶贫项目请求：{}", project.getTitle());
        return projectService.addProject(project);
    }

    /**
     * 更新项目信息
     */
    @PutMapping("/update")
    @Operation(summary = "更新项目信息", description = "更新扶贫项目详细信息")
    public Result<?> updateProject(@Valid @RequestBody Project project) {
        log.info("更新扶贫项目请求：{}", project.getTitle());
        return projectService.updateProject(project);
    }

    /**
     * 项目捐赠
     */
    @PostMapping("/donate")
    @Operation(summary = "项目捐赠", description = "用户对项目进行捐赠")
    public Result<?> donateToProject(@Valid @RequestBody DonationDTO donationDTO) {
        log.info("项目捐赠请求，项目ID：{}，金额：{}", donationDTO.getProjectId(), donationDTO.getAmount());
        return projectService.donateToProject(donationDTO);
    }

    /**
     * 获取捐赠记录
     */
    @GetMapping("/donations/{projectId}")
    @Operation(summary = "获取捐赠记录", description = "获取项目的捐赠记录列表")
    public Result<?> getDonationList(@PathVariable Long projectId,
                                     @RequestParam(defaultValue = "1") Integer pageNum,
                                     @RequestParam(defaultValue = "10") Integer pageSize) {
        return projectService.getDonationList(projectId, pageNum, pageSize);
    }

    /**
     * 获取用户捐赠记录
     */
    @GetMapping("/user/donations/{userId}")
    @Operation(summary = "获取用户捐赠记录", description = "获取用户的所有捐赠记录")
    public Result<?> getUserDonationList(@PathVariable Long userId,
                                         @RequestParam(defaultValue = "1") Integer pageNum,
                                         @RequestParam(defaultValue = "10") Integer pageSize) {
        return projectService.getUserDonationList(userId, pageNum, pageSize);
    }

    /**
     * 获取项目统计
     */
    @GetMapping("/statistics")
    @Operation(summary = "获取项目统计", description = "获取扶贫项目统计信息")
    public Result<?> getProjectStatistics() {
        return projectService.getProjectStatistics();
    }

    /**
     * 删除扶贫项目
     */
    @DeleteMapping("/delete/{projectId}")
    @Operation(summary = "删除扶贫项目", description = "根据项目ID删除对应的扶贫项目")
    public Result<?> deleteProjectById(@PathVariable Long projectId) {
        log.info("删除扶贫项目请求，项目ID：{}", projectId);
        return projectService.deleteProjectById(projectId);
    }


    /**
     * 健康检查
     */
    @GetMapping("/health")
    @Operation(summary = "健康检查", description = "检查服务是否正常运行")
    public Result<?> health() {
        return Result.success("扶贫项目服务运行正常");
    }
} 